<template>
  <div class="app-container">
    <!-- <div class="filter-container">
    </div> -->

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" prop="id" align="center" min-width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平台" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.pushPlatform }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开关" min-width="60" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.pushSwitch === 0" type="danger">关</el-tag>
          <el-tag v-if="row.pushSwitch === 1" type="success">开</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="地区" class-name="status-col" min-width="200">
        <template slot-scope="{row}">
          <span v-for="item in row.list" :key="item.id">
            <el-tag style="margin-left:5px;"> {{ item.name }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="180" align="center">
        <template slot-scope="{row}">
          {{ row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="修改时间" min-width="180" align="center">
        <template slot-scope="{row}">
          {{ row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="pushSettingList"
    />

    <el-dialog :title="textMap[dialogStatus]" :show-close="false" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="conf" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">

        <el-input v-model="conf.id" type="hidden" />
        <el-form-item label="平台">
          <el-input v-model="conf.pushPlatform" readonly="readonly" disabled />
        </el-form-item>
        <el-form-item label="地区">
          <el-input v-model="conf.pushArea" /><span style="color:red">以"，"分割</span>
        </el-form-item>
        <el-form-item label="开关">
          <el-select
            v-model="conf.pushSwitch"
            filterable
            clearable=""
            placeholder="开关"
            style="width: 150px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          >
            <el-option key="1" label="开" :value="1" />
            <el-option key="0" label="关" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateSettingSwitch, pushSettingList } from '@/api/sys'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'PushDataconf',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      props: { multiple: true },
      limitNum: 1,
      fileList: [],
      comment: '',
      tableData: [],
      total: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 10,
        sysLabel: ''
      },
      conf: {
        id: undefined,
        appName: new Date(),
        h5: '',
        slogen: '',
        applyCount: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      dialogPvVisible: false,
      selectedOptions: [],
      downloadLoading: false,
      listLoading: false,
      imageUrl: ''
    }
  },
  created() {
    this.pushSettingList(this.listQuery)
  },
  methods: {
    // 获取列表
    async pushSettingList(listQuery) {
      const res = await pushSettingList(this.listQuery)
      this.tableData = res.data.data.records
      this.total = res.data.data.total
      this.tableData.forEach(element => {
        const ctime = element.createTime
        element.createTime = ctime[0] +
        '-' + ctime[1] +
        '-' + ctime[2] +
        ' ' + ctime[3] +
        ':' + ctime[4] +
        ':' + ctime[5]
        const utime = element.updateTime
        element.updateTime = utime[0] +
        '-' + utime[1] +
        '-' + utime[2] +
        ' ' + utime[3] +
        ':' + utime[4] +
        ':' + utime[5]
        const apps = []
        if (element.pushArea !== null) {
          element.pushArea.split(',').forEach(area => {
            const app = {}
            app.name = area
            apps.push(app)
          })
          element.list = apps
        }
      })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.pushSettingList(this.listQuery)
    },
    cancel() {
      this.dialogFormVisible = false
      this.pushSettingList()
    },
    async handleUpdate(row) {
      this.conf = row
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      const that = this
      this.conf.createTime = null
      this.conf.updateTime = null
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateSettingSwitch(this.conf).then((res) => {
            if (res.data.code === 200) {
              that.$notify({
                title: 'Success',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              that.pushSettingList(that.listQuery)
            } else {
              that.$notify({
                title: 'fail',
                message: '更新失败',
                type: 'fail',
                duration: 2000
              })
            }
            this.dialogFormVisible = false
          })
        }
      })
    }

  }
}
</script>

<style scoped>
.pic{
  width: 150px;
  margin-top: 10px;
  margin-left: 10px;
}
.ok{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color:rgb(35, 248, 124)
}
.fail{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color: rgb(240, 174, 76);
}
.info{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
}
.info-name{
  margin-left: 50px;
  margin-top: 10px;
  color:rgb(172, 176, 177);
}
.title{
  margin-top: 10px;
  margin-left: 10px;
  font-size: 20px;
  font-weight: 400;
}
.box{
  margin-top: 10px;
  width: 100%;
  border: solid rgb(138, 219, 230) 2px;
}
.nick{
  font-size: 30px;
  font-weight: 600;
}
.link-top {
  margin-left: 10px;
  margin-top: 10px;
  width: 100%;
  height: 2px;
  border-top: solid rgb(138, 219, 230) 2px;
}
.link-title {
  margin-left: 10px;
  margin-top: 10px;
  width: 80px;
  height: 2px;
  border-top: solid rgb(172, 176, 177) 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
